﻿//-----------------------------------------------------------------
//
// @(#)$Id: CustomLableDetail.aspx.cs,v 1.0 2013/12/13 16:57:32 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 16:57:32 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ebay.Dal.K3DataBaseTableAdapters;
using ebay.Dal.CenterDB_BasTableAdapters;

public partial class UI_Bas_CustomLableDetail : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        //包裹类型
        T_SubMessageTableAdapter subMessageAdapter = new T_SubMessageTableAdapter();
        ddlPackageId.DataSource = subMessageAdapter.GetDataByFTypeId(10008);
        ddlPackageId.DataTextField = "FName";
        ddlPackageId.DataValueField = "FItemID";
        ddlPackageId.DataBind();
        ddlPackageId.Items.Insert(0, new ListItem("<--请选择-->", ""));

        //发货仓库
        T_StockTableAdapter stockAdapter = new T_StockTableAdapter();
        ddlWh_Code.DataSource = stockAdapter.GetData();
        ddlWh_Code.DataTextField = "FName";
        ddlWh_Code.DataValueField = "FItemId";
        ddlWh_Code.DataBind();
        ddlWh_Code.Items.Insert(0, new ListItem("<--请选择-->", ""));

        //登陆方式
        ddlSellingFormat.Items.Insert(0, new ListItem("<--请选择-->", ""));
        ddlSellingFormat.Items.Insert(1, new ListItem("BG", "BG"));
        ddlSellingFormat.Items.Insert(2, new ListItem("A1", "A1")); 

        //登陆国家        
        ddlLogonCountry.Items.Insert(0, new ListItem("<--请选择-->", ""));
        ddlLogonCountry.Items.Insert(1, new ListItem("UK", "UK"));
        ddlLogonCountry.Items.Insert(2, new ListItem("DE", "DE")); 

    }

    string act = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        act = Request.QueryString["act"];
        if (!IsPostBack)
        {
            switch (act)
            {
                case "I":

                    break;
                case "c":
                case "e":
                    {
                        string customid = Request.QueryString["id"];
                      
                        T_CustomLableTableAdapter adapter = new T_CustomLableTableAdapter();
                        DataTable dt = adapter.GetDataByCustomId(customid);
                        if (dt.Rows.Count > 0)
                        {
                            DataRow r = dt.Rows[0];
                            foreach (Control ctr in form1.Controls)
                            {
                                if (ctr is HtmlInputText)
                                {
                                    HtmlInputText txtbox = (HtmlInputText)ctr;
                                    string name = txtbox.ID.Substring(3, txtbox.ID.Length - 3);
                                    txtbox.Value = r[name].ToString();
                                }
                                else if (ctr is HtmlSelect)
                                {
                                    HtmlSelect ddlbox = (HtmlSelect)ctr;
                                    string name = ddlbox.ID.Substring(3, ddlbox.ID.Length - 3);
                                    ddlbox.Value = r[name].ToString();
                                }
                                else if (ctr is DropDownList)
                                {
                                    DropDownList ddlbox = (DropDownList)ctr;
                                    string name = ddlbox.ID.Substring(3, ddlbox.ID.Length - 3);
                                    ddlbox.Text = r[name].ToString();
                                }
                            }
                        }
                    }
                    break;              
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        DateTime defaultDateTime = new DateTime(1900, 1, 1);
        if (Session["UserId"] == null)
        {
            WebUtility.ShowMsg("登陆超时,请重新登陆", this);
            return;
        }
        string userId = Session["UserId"].ToString();

        if (!string.IsNullOrEmpty(act))
        {
            switch (act)
            {
                case "c":
                case "I":
                    {
                        T_CustomLableTableAdapter adapter = new T_CustomLableTableAdapter();
                        int falg = int.Parse(adapter.IsExists(txtCustomerId.Value).ToString());
                        if (falg > 0)
                        {
                            Response.Write("<script>alert('此CustomerId已经存在...!');</script>");
                        }
                        else
                        {
                            int val = adapter.Insert(
                                             txtCustomerId.Value,
                                             txtListing.Value,
                                             Convert.ToInt32(ddlPackageId.Value),
                                             Convert.ToInt32(ddlWh_Code.Value),
                                             userId,//Muser
                                             System.DateTime.Now,
                                             "",//Auser
                                             defaultDateTime,
                                             ddlSellingFormat.Value,
                                             ddlLogonCountry.Value
                                );
                            if (val > 0)
                            {
                                //Response.Write("<script>alert('添加成功!');window.location.href='CustomLable.aspx'</script>");
                                Response.Write("<script>alert('添加成功!')</script>");
                            }
                        }
                    }
                    break;
                case "e":
                    {
                        string customid = Request.QueryString["id"];
                        if (customid.IndexOf(" ") > 0)
                        {
                            customid = customid.Replace(" ", "+");
                        }
                        T_CustomLableTableAdapter adapter = new T_CustomLableTableAdapter();
                        DataTable dt = adapter.GetDataByCustomId(customid);
                        DataRow r = dt.Rows[0];
                        if (dt.Rows.Count > 0)
                        {
                            foreach (Control ctr in form1.Controls)
                            {
                                if (ctr is HtmlInputText)
                                {
                                    HtmlInputText txtbox = (HtmlInputText)ctr;
                                    string name = txtbox.ID.Substring(3, txtbox.ID.Length - 3);
                                    r[name] = txtbox.Value;
                                }
                                else if (ctr is HtmlSelect)
                                {
                                    HtmlSelect ddlbox = (HtmlSelect)ctr;
                                    string name = ddlbox.ID.Substring(3, ddlbox.ID.Length - 3);
                                    r[name] = ddlbox.Value;
                                }
                                else if (ctr is DropDownList)
                                {
                                    DropDownList ddlbox = (DropDownList)ctr;
                                    string name = ddlbox.ID.Substring(3, ddlbox.ID.Length - 3);
                                    r[name] = ddlbox.SelectedValue;
                                }
                            }
                        }
                        r["Muser"] = userId;
                        r["Mtime"] = System.DateTime.Now;
                        r["Auser"] = "";
                        r["Atime"] = System.DateTime.Now;
                        int val = adapter.Update(r);
                        if (val > 0)
                        {
                            Response.Write("<script>alert('修改成功!');window.location.href='CustomLable.aspx'</script>");
                            //Response.Write("<script>alert('修改成功!')</script>");
                        }
                    }
                    break;
            }
        }
    }   
}